﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using Newtonsoft.Json;
using System.Configuration;
using System.Data;

namespace PromotionEngine
{
    public partial class Doc_Line_Property_Link : System.Web.UI.Page
    {
        public class Response
        {
            public List<Model.DocLinePropertyLink> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public Response(List<Model.DocLinePropertyLink> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public Response(string errors)
            {
                this.Errors = errors;
            }

            public Response()
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string lineNum = Request.QueryString["LineNum"];
            string docStatus = Request.QueryString["DocStatus"];
            this.lblLineNum.Text = lineNum;
            this.hdnDocStatus.Value = docStatus;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static Response GetDocLinePropertyLinks()
        {
            List<Model.DocLinePropertyLink> lstDocLineProperty = new List<Model.DocLinePropertyLink>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllDocLinePropertyLink(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString());
            if (dsPromotion != null)
            {

                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.DocLinePropertyLink docLinePropertyLink = new Model.DocLinePropertyLink();
                    docLinePropertyLink.PropertyLinkID = r["PropertyLinkID"].ToString();
                    docLinePropertyLink.LineNum = r["LineNum"].ToString();
                    docLinePropertyLink.PropertyDetailID = r["PropertyDetailID"].ToString();
                    docLinePropertyLink.PropertyDetailCode = r["PropertyDetailCode"].ToString();
                    docLinePropertyLink.PropertyDetailName = r["PropertyDetailName"].ToString();
                    lstDocLineProperty.Add(docLinePropertyLink);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstDocLineProperty };
                return new Response(lstDocLineProperty, dsPromotion.Tables[0].Rows.Count);
            }
            return new Response();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateDocLinePropertyLink(string oDocLinePropertyLink, bool isInsert)
        {
            Model.DocLinePropertyLink docLinePropertyLink = JsonConvert.DeserializeObject<Model.DocLinePropertyLink>(oDocLinePropertyLink);

            Promotion pmt = new Promotion();
            string errMsg = pmt.UpdateJsonDocLinePropertyLink(docLinePropertyLink.LineNum, docLinePropertyLink.PropertyDetailID, docLinePropertyLink.PropertyLinkID,
                  ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static Response GetDocLinePropertyLinkByDoc(string lineNum, string propertyID)
        {
            List<Model.DocLinePropertyLink> lstDocLineProperty = new List<Model.DocLinePropertyLink>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllDocLinePropertyLinkByItem(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), propertyID, lineNum);
            if (dsPromotion != null)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.DocLinePropertyLink docLinePropertyLink = new Model.DocLinePropertyLink();
                    docLinePropertyLink.PropertyLinkID = r["PropertyLinkID"].ToString();
                    docLinePropertyLink.LineNum = r["LineNum"].ToString();
                    docLinePropertyLink.PropertyDetailID = r["PropertyDetailID"].ToString();
                    docLinePropertyLink.PropertyDetailCode = r["PropertyDetailCode"].ToString();
                    docLinePropertyLink.PropertyDetailName = r["PropertyDetailName"].ToString();
                    lstDocLineProperty.Add(docLinePropertyLink);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstDocLineProperty };
                return new Response(lstDocLineProperty, dsPromotion.Tables[0].Rows.Count);
            }
            return new Response();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateDocLinePropertyLinks(string oDocLinePropertyLink, bool isInsert)
        {
            List<Model.DocLinePropertyLink> docLinePropertyLinks = JsonConvert.DeserializeObject<List<Model.DocLinePropertyLink>>(oDocLinePropertyLink);

            Promotion pmt = new Promotion();
            foreach (Model.DocLinePropertyLink docLinePropertyLink in docLinePropertyLinks)
            {
                string errMsg = pmt.UpdateJsonDocLinePropertyLink(docLinePropertyLink.LineNum, docLinePropertyLink.PropertyDetailID, docLinePropertyLink.PropertyLinkID,
                      ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
                if (errMsg.Length == 0)
                {
                    //Show error message
                }
            }
        }
    }
}
